package com.human.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.human.server.entity.*;
import com.human.server.enumration.AssessmentStatus;
import com.human.server.enumration.FillDetailStatus;
import com.human.server.enumration.FillStatus;
import com.human.server.mapper.*;
import com.human.server.model.request.AssessmentFillsRequest;
import com.human.server.model.request.AssessmentsAddRequest;
import com.human.server.service.AssessmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author Admin
* @description 针对表【assessments(考核方案表)】的数据库操作Service实现
* @createDate 2025-09-08 00:49:41
*/
@Service
public class AssessmentsServiceImpl extends ServiceImpl<AssessmentsMapper, Assessments>
    implements AssessmentsService{

    @Autowired
    private AssessmentProcessStepsMapper assessmentProcessStepsMapper;

    @Autowired
    private AssessmentKpisMapper assessmentKpisMapper;

    @Autowired
    private KpisMapper kpisMapper;

    @Autowired
    private AssessmentFillsMapper assessmentFillsMapper;

    @Autowired
    private KpiFillDetailsMapper kpiFillDetailsMapper;

    @Autowired
    private EmployeesMapper employeesMapper;

    public List<AssessmentProcessSteps> steps(Assessments assessments) {

        return assessmentProcessStepsMapper.selectList(new LambdaQueryWrapper<AssessmentProcessSteps>()
                .eq(AssessmentProcessSteps::getAssessmentId, assessments.getId()));
    }

    public void add(AssessmentsAddRequest assessmentsAddRequest) {

        Assessments assessments = assessmentsAddRequest;
        this.save(assessments);
        List<Kpis> kpisList = assessmentsAddRequest.getKpis();

        // 删除关联数据
        assessmentKpisMapper.delete(new LambdaQueryWrapper<AssessmentKpis>()
                .eq(AssessmentKpis::getAssessmentId, assessments.getId()));
        for (Kpis kpis : kpisList) {
            AssessmentKpis assessmentKpis = new AssessmentKpis();
            assessmentKpis.setAssessmentId(assessments.getId());
            assessmentKpis.setKpiId(kpis.getId());
            assessmentKpis.setWeight(kpis.getWeight());
            assessmentKpis.setName(kpis.getName());
            assessmentKpisMapper.insert(assessmentKpis);
        }

    }

    public void updateAssessments(AssessmentsAddRequest assessmentsAddRequest) {

        Assessments assessments = assessmentsAddRequest;
        this.updateById(assessments);
        List<Kpis> kpisList = assessmentsAddRequest.getKpis();

        // 删除关联数据
        assessmentKpisMapper.delete(new LambdaQueryWrapper<AssessmentKpis>()
                .eq(AssessmentKpis::getAssessmentId, assessments.getId()));
        for (Kpis kpis : kpisList) {
            AssessmentKpis assessmentKpis = new AssessmentKpis();
            assessmentKpis.setAssessmentId(assessments.getId());
            assessmentKpis.setKpiId(kpis.getId());
            assessmentKpis.setWeight(kpis.getWeight());
            assessmentKpis.setName(kpis.getName());
            assessmentKpisMapper.insert(assessmentKpis);
        }
    }

    public List<Kpis> kpis(Assessments assessments) {

        // 查询关联的kpi
        List<AssessmentKpis> assessmentKpisList = assessmentKpisMapper.selectList(new LambdaQueryWrapper<AssessmentKpis>()
                .eq(AssessmentKpis::getAssessmentId, assessments.getId()));

        // 查询kpi详情
        List<Kpis> kpisList = kpisMapper.selectBatchIds(assessmentKpisList.stream().map(AssessmentKpis::getKpiId).collect(Collectors.toList()));

        return kpisList;
    }

    public void publish(Assessments assessments) {

        assessments.setStatus(AssessmentStatus.IN_PROGRESS.getValue());

        // 查询数据库重点考勤
        Assessments localAssessment = this.getById(assessments.getId());

        String department = localAssessment.getDepartment();
        // 逗号分割，部门id列表
        List<String> departmentList = Arrays.asList(department.split(","));

        for (String departmentId : departmentList) {
            // 查出部门下的所有员工
            List<Employees> employeesList = employeesMapper.selectList(new LambdaQueryWrapper<Employees>()
                    .eq(Employees::getDepartmentId, departmentId));

            employeesList.forEach(employees -> {
                AssessmentFills assessmentFills = new AssessmentFills();
                assessmentFills.setAssessmentId(localAssessment.getId());
                assessmentFills.setAssessmentName(localAssessment.getName());
                assessmentFills.setEmployeeId(employees.getId());
                assessmentFills.setEmployeeName(employees.getName());
                assessmentFills.setStartDate(localAssessment.getStartDate());
                assessmentFills.setEndDate(localAssessment.getEndDate());
                assessmentFills.setMonth(localAssessment.getMonth());
                assessmentFills.setDepartmentId(Integer.parseInt(departmentId));
                assessmentFills.setFillStatus(FillStatus.NOT_STARTED.getValue());
                assessmentFillsMapper.insertOrUpdate(assessmentFills);

                // 同步插入kpis明细表
                List<AssessmentKpis> assessmentKpisList = assessmentKpisMapper.selectList(new LambdaQueryWrapper<AssessmentKpis>()
                        .eq(AssessmentKpis::getAssessmentId, localAssessment.getId()));
                for (AssessmentKpis assessmentKpis : assessmentKpisList) {
                    KpiFillDetails kpiFillDetails = new KpiFillDetails();
                    kpiFillDetails.setFillId(assessmentFills.getId());
                    kpiFillDetails.setKpiId(assessmentKpis.getKpiId());
                    kpiFillDetails.setKpiName(assessmentKpis.getName());
                    kpiFillDetails.setKpiWeight(assessmentKpis.getWeight());
                    kpiFillDetails.setStatus(FillDetailStatus.NOT_STARTED.getValue());
                    kpiFillDetailsMapper.insertOrUpdate(kpiFillDetails);
                }
            });
        }

        this.updateById(assessments);
    }
}




